local skel = fk.CreateSkill {
  name = "emo__jinfu",
}

Fk:loadTranslationTable{
  ["emo__jinfu"] = "禁符",
  [":emo__jinfu"] = "你可以令你无法使用同名牌以视为对其他角色使用的普通锦囊使用【无懈可击】。你因此禁止的牌可以当【杀】或【闪】使用或打出。",

  ["@$emo__jinfu"] = "禁符",
  ["#emo__jinfu-nullify"] = "禁符:你可令你无法使用【%arg】，视为对%dest的【%arg】使用【无懈】",
  ["#emo__jinfu-use"] = "将被“禁符”禁止使用的锦囊牌当【杀】或【闪】使用或打出",
  ["#emo__jinfu-slash"] = "将被“禁符”禁止使用的锦囊牌当【杀】使用",
  ["#emo__jinfu-prohibit"] = "禁符：选择一个锦囊牌名，令你无法使用之",
}

--- 获取可以被禁止使用的牌名（废案）
---@param player Player
local function getTrickNames (player)
  local names = Fk:getAllCardNames("td", true)
  local mark = player:getTableMark("@$emo__jinfu")
  table.removeOne(names, "nullification")
  return table.filter(names, function (name) return not table.contains(mark, name) end)
end

skel:addEffect("viewas", {
  anim_type = "control",
  pattern = "slash,jink,nullification",
  prompt = function (self, player, selected_cards, selected)
    local pat = Fk.currentResponsePattern
    if pat == "nullification" then
      local mark = player:getMark("emo__jinfu_nullify")
      if mark == 0 then return " " end
      return "#emo__jinfu-nullify::" .. mark[1] .. ":" .. mark[2]
    elseif pat == nil then
      return "#emo__jinfu-slash"
    end
    return "#emo__jinfu-use"
  end,
  interaction = function (self, player)
    -- 如果要印无懈就不打开框了
    if Fk.currentResponsePattern == "nullification" then
      return nil
    end
    local all_choices = {"slash", "jink"}
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 and self.interaction.data then
      return table.contains(player:getTableMark("@$emo__jinfu"), Fk:getCardById(to_select).trueName)
    end
    return false
  end,
  view_as = function (self, player, cards)
    if self.interaction.data == nil then
      if Fk.currentResponsePattern == "nullification" then
        local c = Fk:cloneCard("nullification")
        c.skillName = skel.name
        return c
      end
    elseif #cards == 1 then
      local c = Fk:cloneCard(self.interaction.data)
      c.skillName = skel.name
      c:addSubcards(cards)
      return c
    end
  end,
  before_use = function(self, player, use)
    local room = player.room
    if use.card.name == "nullification" then
      --[[
      local choices = getTrickNames(player)
      if #choices == 0 then return self.name end
      local choice = room:askToChoice(player, {
        choices = choices, skill_name = skel.name, prompt = "#emo__jinfu-prohibit",
      })
      --]]
      -- 数据没法直接传进来呀，有点危险
      local current_event = room.logic:getCurrentEvent():findParent(GameEvent.CardEffect, true)
      if current_event == nil then return skel.name end
      local choice = current_event.data.card.name
      room:addTableMark(player, "@$emo__jinfu", choice)
    end
  end,
  enabled_at_play = function(self, player)
    return #player:getTableMark("@$emo__jinfu") > 0 and player:canUse(Fk:cloneCard("slash"))
  end,
  enabled_at_response = function (self, player, response)
    local pattern = Fk.currentResponsePattern
    if not response and pattern == "nullification" then
      return player:getMark("emo__jinfu_nullify") ~= 0
    end
    return #player:getTableMark("@$emo__jinfu") > 0 and Exppattern:Parse(pattern):matchExp("jink,slash")
  end,
  enabled_at_nullification = function (self, player, data)
    return data and data.from and data.from ~= player and data.card and data.card:isCommonTrick()
    and not table.contains(player:getTableMark("@$emo__jinfu"), data.card.name)
  end,
})

skel:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    return card and player and table.contains(player:getTableMark("@$emo__jinfu"), card.trueName)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@$emo__jinfu", 0)
end)

skel:addEffect(fk.HandleAskForPlayCard, {
  can_refresh = function(self, event, target, player, data)
    if data.afterRequest and (data.extra_data or {}).emo__jinfu_effected then
      return player:getMark("emo__jinfu_nullify") ~= 0
    end

    return
      player:hasSkill(skel.name) and
      data.eventData and
      data.eventData.from and
      data.eventData.from ~= player and
      data.eventData.card:isCommonTrick() and
      Exppattern:Parse(data.pattern):matchExp("nullification")
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if data.afterRequest then
      room:setPlayerMark(player, "emo__jinfu_nullify", 0)
    else
      room:setPlayerMark(player, "emo__jinfu_nullify", {data.eventData.from.id, data.eventData.card.name})
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__jinfu_effected = true
    end
  end,
})


return skel
